import { findAll, del, add, edit } from '@/api/userLogin'
import store from "@/store";
import {decryptStr, powerSelect} from "@/api/user";
import fa from "element-ui/src/locale/lang/fa";
export default {
  name:'User',
  data() {
    return {
      tableData: [],
      dialogFormVisible: false,
      formLabelWidth: '120px',
      form: {
        id:'',
        username:'',
        password:'',
        name: '',
        department:'',
        departmentJob:'',
        phone:'',
        power:'',
      },
      //权限校验
      show:true,
      // 表单校验规则
      rules: {
        username: [
          { required: true, message: '*用户名必填', trigger: 'blur' },
        ],
        password: [
          { required: true, message: '*密码必填', trigger: 'blur' },
          { min: 6, max: 16, message: '长度在 6 到 16 个字符', trigger: 'blur' }
        ],
        name: [
          { required: true, message: '姓名必填', trigger: 'blur' },
        ],
        department: [
          { required: true, message: '部门必填', trigger: 'blur' },
        ],
        departmentJob: [
          { required: true, message: '岗位必填', trigger: 'blur' },
        ],
        phone: [
          { required: true, message: '电话必填', trigger: 'blur' },
        ],
      },
      //分页默认选中
      currentPage: 1,
      //分页总数据数
      total:0,
      //分页查询每页查询的数量
      pageNum:1,
      pageSize:5,
      // 模糊搜索
      formInline: {
        user: '',
        department:'',
        creaTime:''
      },
      userInfo:{
        pwId:'',
        pwName:'',
        pwLevel:'',
        pwTip:''
      },
      // 用于控制插入/新增的属性
      news:false,
      dialogExcelVisible:false
    }
  },

  methods: {
    //编辑
    handleEdit(index, row) {
      this.userInfo.pwLevel = ''
      if (store.getters.powerCheck.powerLevel == ""){
        this.$message({
          message: '登录过期，请重新登录',
          type: 'error'
        });
      }else if (store.getters.powerCheck.powerLevel == 100){
        this.edits(index,row)
      }else if (store.getters.powerCheck.powerLevel == 200){
        this.show = false
        let data = {
          lgId: row.lgId
        }
        powerSelect(data).then(res =>{
          this.userInfo = res.data.power
          if (this.userInfo.pwLevel === 100){
            this.$message({
              message: '权限越界，非超管禁止修改超管内容',
              type: 'error'
            });
          }else {
            this.edits(index,row)
          }
        })
      }else if (store.getters.powerCheck.powerLevel == 300){
        this.$message({
          message: '你所在的用户组禁止修改本界面',
          type: 'error'
        });
      }else {
        this.$message({
          message: '权限非法，请联系管理员',
          type: 'error'
        });
      }
    },
    // 删除单个
    handleDelete(index, row) {
      this.userInfo.pwLevel = ''
      if (store.getters.powerCheck.powerLevel == ""){
        this.$message({
          message: '登录过期，请重新登录',
          type: 'error'
        });
      }else if (store.getters.powerCheck.powerLevel == 100){
        this.del(index,row)
      }else if (store.getters.powerCheck.powerLevel == 200){
        let data = {
          lgId: row.lgId
        }
        powerSelect(data).then(res =>{
          this.userInfo = res.data.power
          if (this.userInfo.pwLevel === 100){
            this.$message({
              message: '权限越界，非超管禁止修改超管内容',
              type: 'error'
            });
          }else {
            this.del(index,row)
          }
        })
      }else if (store.getters.powerCheck.powerLevel == 300){
        this.$message({
          message: '你所在的用户组禁止修改本界面',
          type: 'error'
        });
      }else {
        this.$message({
          message: '权限非法，请联系管理员',
          type: 'error'
        });
      }
    },
    // 查全部
    findAll(){
      //权限校验
      //这里访问的是getters状态树
      if (store.getters.powerCheck.name == "") {
        this.$message({
          message: '登录过期，请重新登录',
          type: 'error'
        });
      }else {
        this.powerChecks()
      }
      let params = {
        pageNum:this.pageNum,
        pageSize:this.pageSize,
        lgName: this.formInline.user,
        lgDepartment: this.formInline.department,
        lgCreatTime:this.formInline.creaTime
      }
      findAll(params).then(res =>{
        if (res.data != null){
          this.$message({
            message: '请求成功',
            type: 'success'
          });
          this.total = res.data.total
          this.tableData = res.data.list
        }else {
          this.$message({
            message: '请求失败',
            type: 'error'
          });
        }
        // todo 出错后重定向到404界面，有时间就搞，没时间算求
      })
    },
    // 插入/编辑确定按钮
    submit(formName){
      let data = {
        lgId:this.form.id,
        lgUsername:this.form.username,
        lgPassword:this.form.password,
        lgName:this.form.name,
        lgDepartment:this.form.department,
        lgDepartmentJob:this.form.departmentJob,
        lgPhone:this.form.phone,
        lgPower:this.form.power,
      }
      this.$refs[formName].validate((valid) => {
        if (this.news == false){
          edit(data).then(res =>{
            if (res.data >0){
              this.$message({
                message: '修改成功',
                type: 'success'
              });
              this.findAll();
              this.dialogFormVisible = false
            }else {
              this.$message({
                message: '修改失败',
                type: 'error'
              });
            }
          })
        }else {
          if (valid) {
            add(data).then(res =>{
              if (res.data >0){
                this.$message({
                  message: '插入成功',
                  type: 'success'
                });
              }
              this.findAll()
            })
            this.dialogFormVisible = false;
          } else {
            console.log('error submit!!');
            return false;
          }
        }
      });
    },
    // 分页按钮
    handleSizeChange(val) {
      this.pageSize = val
      this.findAll()
    },
    handleCurrentChange(val) {
      this.pageNum = val
      this.findAll()
    },
    // 模糊查询
    search(){
      this.findAll()
    },
    // 模糊查询重置
    reset(){
      this.formInline = {}
    },
    //新增按钮
    add(){
      //权限校验
      //这里访问的是getters状态树
      if (store.getters.powerCheck.powerLevel == ""){
        this.$message({
          message: '登录过期，请重新登录',
          type: 'error'
        });
      }else if (store.getters.powerCheck.powerLevel == 100){
        this.dialogFormVisible = true
        this.news = true;
        this.show = true
        this.form = {
          power: 1
        }
      }else if (store.getters.powerCheck.powerLevel == 200){
        this.dialogFormVisible = true
        this.news = true;
        this.show = false
        this.form = {
          power: 2
        }
      }else if (store.getters.powerCheck.powerLevel == 300){
        this.$message({
          message: '你所在的用户组禁止修改本界面',
          type: 'error'
        });
      }else {
        this.$message({
          message: '权限非法，请联系管理员',
          type: 'error'
        });
      }
    },
    powerChecks(){
      let data = {
        lgUsername: store.getters.powerCheck.name,
        lgPassword: store.getters.powerCheck.password
      }
      powerSelect(data).then(res => {
        console.log(data)
         store.dispatch("powerCheck/setPower", res.data.power.pwLevel);
      })
    },
    //编辑弹框复用
    edits(index,row){
      this.dialogFormVisible=true
      this.news = false
      let params = {
        password:row.lgPassword
      }
      decryptStr(params).then(res =>{
        this.form = {
          id:row.lgId,
          username:row.lgUsername,
          password:res.data,
          name:row.lgName,
          department: row.lgDepartment,
          departmentJob: row.lgDepartmentJob,
          phone:row.lgPhone,
          power:row.lgPower
        }
      })
    },
    //删除代码复用
    del(index,row){

      let id = {
        lgId:row.lgId
      }
      this.$confirm('此操作将永久删除ID为'+row.lgId+'的数据 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        del(id).then(res =>{
          if (res.data >0){
            this.$message({
              type: 'success',
              message: '删除成功!'
            });
            this.findAll();
          }else {
            this.$message({
              type: 'error',
              message: '删除失败!'
            });
          }
        })
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        });
      });
    },
    downloadExcel(){
      this.$confirm('你确定要导出数据吗？', '信息', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        this.$message({
          type: 'success',
          message: '导出成功!'
        });
        window.location.href = "http://localhost:8088/user/download/excel"
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消导出'
        });
      });
    },
    uploadExcelSuccess(){
      this.$message({
        type:"success",
        message:"上传成功"
      })
      this.dialogExcelVisible = false;
      this.fetchData();
    },
    uploadExcelError(err){
      this.$message({
        type:"error",
        message:err
      })
    }
  },
  created() {
    this.findAll();
  },
}
